﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using Tesla.Core;

namespace Tesla.Graphics {
    /// <summary>
    /// Render Target that is an array of 6 Texture2D's that are put together in a Cube.
    /// </summary>
    public class RenderTargetCube : TextureCube {

        /// <summary>
        /// Gets the format of the depth-stencil buffer.
        /// </summary>
        public DepthFormat DepthStencilFormat {
            get {
                return _impl.DepthFormat;
            }
        }

        /// <summary>
        /// Gets the multisample count used during multisampling.
        /// </summary>
        public int MultiSampleCount {
            get {
                return _impl.MultiSampleCount;
            }
        }

        /// <summary>
        /// Gets the render target usage.
        /// </summary>
        public RenderTargetUsage TargetUsage {
            get {
                return _impl.TargetUsage;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="RenderTargetCube"/>.
        /// </summary>
        /// <param name="size">The size (width/height) of each cube face.</param>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying render target fails or the render
        /// system was not initialized.</exception>
        public RenderTargetCube(int size) {
            IRenderSystemProvider renderSystem = Engine.Services.GetService<IRenderSystemProvider>();
            if(renderSystem == null) {
                Dispose();
                throw new TeslaException("Render system provider not set, cannot create graphics resource implementation.");
            }

            base.RenderSystem = renderSystem;

            try {
                _impl = renderSystem.CreateRenderTargetCubeImplementation(size, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.DiscardContents);
            } catch(Exception e) {
                Dispose();
                throw new TeslaException("Error creating underlying implementation, see inner exception for details.", e);
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="RenderTargetCube"/>.
        /// </summary>
        /// <param name="size">The size (width/height) of each cube face.</param>
        /// <param name="genMipMap">True if mip levels should be generated.</param>
        /// <param name="format">The surface format.</param>
        /// <param name="depthFormat">The depth format</param>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying render target fails or the render
        /// system was not initialized.</exception>
        public RenderTargetCube(int size, bool genMipMap, SurfaceFormat format, DepthFormat depthFormat) {
            IRenderSystemProvider renderSystem = Engine.Services.GetService<IRenderSystemProvider>();
            if(renderSystem == null) {
                Dispose();
                throw new TeslaException("Render system provider not set, cannot create graphics resource implementation.");
            }

            base.RenderSystem = renderSystem;

            try {
                _impl = renderSystem.CreateRenderTargetCubeImplementation(size, genMipMap, format, depthFormat, 0, RenderTargetUsage.DiscardContents);
            } catch(Exception e) {
                Dispose();
                throw new TeslaException("Error creating underlying implementation, see inner exception for details.", e);
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="RenderTargetCube"/>.
        /// </summary>
        /// <param name="size">The size (width/height) of each cube face.</param>
        /// <param name="genMipMap">True if a mipmap chain should be generated or not.</param>
        /// <param name="format">The surface  format.</param>
        /// <param name="depthFormat">The depth-stencil format.</param>
        /// <param name="multiSampleCount">The number of sample locations for multisampling</param>
        /// <param name="usage">Sets the render target's behavior.</param>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying render target fails or the render
        /// system was not initialized.</exception>
        public RenderTargetCube(int size, bool genMipMap, SurfaceFormat format, DepthFormat depthFormat, int multiSampleCount, RenderTargetUsage usage) {
            IRenderSystemProvider renderSystem = Engine.Services.GetService<IRenderSystemProvider>();
            if(renderSystem == null) {
                Dispose();
                throw new TeslaException("Render system provider not set, cannot create graphics resource implementation.");
            }

            base.RenderSystem = renderSystem;

            try {
                _impl = renderSystem.CreateRenderTargetCubeImplementation(size, genMipMap, format, depthFormat, multiSampleCount, usage);
            } catch(Exception e) {
                Dispose();
                throw new TeslaException("Error creating underlying implementation, see inner exception for details.", e);
            }
        }


        /// <summary>
        /// Creates a new instance of <see cref="RenderTargetCube"/>.
        /// </summary>
        /// <param name="renderSystem">Render system used to create the underlying implementation.</param>
        /// <param name="size">The size (width/height) of each cube face.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the render system is null.</exception>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying texture fails.</exception>
        public RenderTargetCube(IRenderSystemProvider renderSystem, int size) {
            if(renderSystem == null) {
                Dispose();
                throw new ArgumentNullException("renderSystem", "Render system cannot be null.");
            }

            base.RenderSystem = renderSystem;

            try {
                _impl = renderSystem.CreateRenderTargetCubeImplementation(size, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.DiscardContents);
            } catch(Exception e) {
                Dispose();
                throw new TeslaException("Error creating underlying implementation, see inner exception for details.", e);
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="RenderTargetCube"/>.
        /// </summary>
        /// <param name="renderSystem">Render system used to create the underlying implementation.</param>
        /// <param name="size">The size (width/height) of each cube face.</param>
        /// <param name="genMipMap">True if mip levels should be generated.</param>
        /// <param name="format">The surface format.</param>
        /// <param name="depthFormat">The depth format</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the render system is null.</exception>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying texture fails.</exception>
        public RenderTargetCube(IRenderSystemProvider renderSystem, int size, bool genMipMap, SurfaceFormat format, DepthFormat depthFormat) {
            if(renderSystem == null) {
                Dispose();
                throw new ArgumentNullException("renderSystem", "Render system cannot be null.");
            }

            base.RenderSystem = renderSystem;

            try {
                _impl = renderSystem.CreateRenderTargetCubeImplementation(size, genMipMap, format, depthFormat, 1, RenderTargetUsage.DiscardContents);
            } catch(Exception e) {
                Dispose();
                throw new TeslaException("Error creating underlying implementation, see inner exception for details.", e);
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="RenderTargetCube"/>.
        /// </summary>
        /// <param name="renderSystem">Render system used to create the underlying implementation.</param>
        /// <param name="size">The size (width/height) of each cube face.</param>
        /// <param name="genMipMap">True if a mipmap chain should be generated or not.</param>
        /// <param name="format">The surface  format.</param>
        /// <param name="depthFormat">The depth-stencil format.</param>
        /// <param name="multiSampleCount">The number of sample locations for multisampling</param>
        /// <param name="usage">Sets the render target's behavior.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the render system is null.</exception>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying texture fails.</exception>
        public RenderTargetCube(IRenderSystemProvider renderSystem, int size, bool genMipMap, SurfaceFormat format, DepthFormat depthFormat, int multiSampleCount, RenderTargetUsage usage) {
            if(renderSystem == null) {
                Dispose();
                throw new ArgumentNullException("renderSystem", "Render system cannot be null.");
            }

            base.RenderSystem = renderSystem;

            try {
                _impl = renderSystem.CreateRenderTargetCubeImplementation(size, genMipMap, format, depthFormat, multiSampleCount, usage);
            } catch(Exception e) {
                Dispose();
                throw new TeslaException("Error creating underlying implementation, see inner exception for details.", e);
            }
        }

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="RenderTargetCube"/> is reclaimed by garbage collection.
        /// </summary>
        ~RenderTargetCube() {
            Dispose(false);
        }
    }
}
